package com.appoint.web.controller.buss;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appoint.common.annotation.Anonymous;
import com.appoint.framework.web.domain.server.SysFile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.appoint.common.annotation.Log;
import com.appoint.common.core.controller.BaseController;
import com.appoint.common.core.domain.AjaxResult;
import com.appoint.common.enums.BusinessType;
import com.appoint.buss.domain.WxInfConfig;
import com.appoint.buss.service.IWxInfConfigService;
import com.appoint.common.utils.poi.ExcelUtil;
import com.appoint.common.core.page.TableDataInfo;

/**
 * 参数配置Controller
 *
 * @author appoint
 * @date 2022-12-03
 */
@RestController
@RequestMapping("/buss/config")
public class WxInfConfigController extends BaseController
{
    @Autowired
    private IWxInfConfigService wxInfConfigService;

    @Value(value = "${epipara.filePath}")
    private String basePath;

    /**
     * 查询参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('buss:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxInfConfig wxInfConfig)
    {
        startPage();
        List<WxInfConfig> list = wxInfConfigService.selectWxInfConfigList(wxInfConfig);
        return getDataTable(list);
    }

    /**
     * 导出参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('buss:config:export')")
    @Log(title = "参数配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxInfConfig wxInfConfig)
    {
        List<WxInfConfig> list = wxInfConfigService.selectWxInfConfigList(wxInfConfig);
        ExcelUtil<WxInfConfig> util = new ExcelUtil<WxInfConfig>(WxInfConfig.class);
        util.exportExcel(response, list, "参数配置数据");
    }

    /**
     * 获取参数配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('buss:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(wxInfConfigService.selectWxInfConfigById(id));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('buss:config:add')")
    @Log(title = "参数配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxInfConfig wxInfConfig)
    {
        return toAjax(wxInfConfigService.insertWxInfConfig(wxInfConfig));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('buss:config:edit')")
    @Log(title = "参数配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxInfConfig wxInfConfig)
    {
        return toAjax(wxInfConfigService.updateWxInfConfig(wxInfConfig));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('buss:config:remove')")
    @Log(title = "参数配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wxInfConfigService.deleteWxInfConfigByIds(ids));
    }
    /**
     * 下载文件
     */
    @Anonymous
    @GetMapping("/downLoadFile")
    public void downLoadFile(HttpServletResponse response, HttpServletRequest request, WxInfConfig wxInfConfig) {
        response.setHeader("Content-Type", "application/octet-stream");
        OutputStream out = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String filePath = basePath+wxInfConfig.getExtend1();
        String fileName = sdf.format(new Date()) + filePath.substring(filePath.lastIndexOf("."));
        try {
            File file = new File(filePath);
            response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
            FileInputStream fin = new FileInputStream(file);
            byte[] data = new byte[(int) file.length()];
            fin.read(data);
            fin.close();
            out = response.getOutputStream();
            out.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 查询参数配置列表
     */
    @Anonymous
    @GetMapping("/list2")
    public TableDataInfo list2(WxInfConfig wxInfConfig)
    {
        startPage();
        List<WxInfConfig> list = wxInfConfigService.selectWxInfConfigList(wxInfConfig);
        return getDataTable(list);
    }
}
